package com.bihu.bihudemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bihu.bihudemo.entity.Comment;
import com.bihu.bihudemo.entity.CommentLike;
import com.bihu.bihudemo.result.Result;
import com.bihu.bihudemo.service.CommentService;
import com.bihu.bihudemo.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Comment)表控制层
 *
 * @author makejava
 * @since 2020-07-15 17:10:17
 */
@RestController
@RequestMapping("comment")
@Api(tags = "评论相关操作")
public class CommentController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;

    @Resource
    private RedisService redisService;

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param comment 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Comment> page, Comment comment) {
        return success(this.commentService.page(page, new QueryWrapper<>(comment)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.commentService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param comment 实体对象
     * @return 新增结果
     */
    @PostMapping("/doComment")
    public R insert(@RequestBody Comment comment) {

        // 被评论的文章评论数+1


        return success(this.commentService.save(comment));
    }


    //点赞操作
    @ApiOperation("点赞操作")
    @PostMapping("/like")
    public Result doLike(@RequestBody CommentLike commentLike){

        //到redis生成一条点赞信息（用户要是第一次点赞这篇评论的话，并设置点赞状态为1）,要是有这条记录就啥也没发生
        redisService.saveLiked2Redis(String.valueOf(commentLike.getCid()),String.valueOf(commentLike.getUid()));
        //将该评论的点赞数+1，并放到redis
        redisService.incrementLikedCount(String.valueOf(commentLike.getCid()));
        return Result.ok();
    }
    //取消点赞操作
    @ApiOperation("踩赞操作")
    @PostMapping("/dislike")
    public Result doDisLike(@RequestBody CommentLike commentLike){
        redisService.unlikeFromRedis(String.valueOf(commentLike.getCid()),String.valueOf(commentLike.getUid()));
        redisService.decrementLikedCount(String.valueOf(commentLike.getCid()));
        return Result.ok();
    }



    //根据文章ID获取评论
    @ApiOperation("根据文章ID获取评论")
    @GetMapping("/getByAid")
    public Result getCommentByCid( @RequestParam("aid") int aid){

        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",aid);
        List<Comment> comments = commentService.list(queryWrapper);
        return  Result.ok().put("commentlist",comments);
    }

    /**
     * 修改数据
     *
     * @param comment 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改评论")
    @PutMapping
    public R update(@RequestBody Comment comment) {
        return success(this.commentService.updateById(comment));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.commentService.removeByIds(idList));
    }
}